package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.entity.SysUser;
import com.ibiaoke.boss.service.LoginService;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.response.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by huhaichao on 2017/6/6.
 */
@Controller
public class LoginController extends BaseController{

    @Autowired
    private LoginService loginService;


    /**
     * 登陆
     * @param userName
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public Res login(String userName, String password) {
        SysUser user = loginService.login(userName, password);
        getSession().setAttribute(CommonConsts.LOGIN_SESSION_NAME,user);
        return Res.create().addResponse("userinfo",user);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Res logout(HttpServletRequest request) {
       getSession().invalidate();
       return Res.create();
    }

    /**
     * 注册用户
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/RegisterUser")
    @ResponseBody
    public Res RegisterUser(String username, String password) {
        loginService.RegisterUser(username, password);
        return Res.create();
    }

}
